﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

using Autodesk.AutoCAD.Interop;

namespace Cad2018Tool
{
    public class ConnectCad
    {
        string strProgId = "AutoCAD.Application.22";
        public ConnectCad()
        {

#if Cad2014
            strProgId = "AutoCAD.Application.19.1";
#elif Cad2018
        strProgId = "AutoCAD.Application.22";
#endif
            // Get a running instance of AutoCAD
            try
            {
                _cad = (AcadApplication)Marshal.GetActiveObject(strProgId);
            }
            catch // An error occurs if no instance is running
            {
                try
                {
                    // Create a new instance of AutoCAD
                    // _cad = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

        }

        private AcadApplication _cad = null;
        public AcadApplication CadApplication()
        {
            return _cad;
        }
    }
}
